package org.jeecg.modules.appuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pdd.pop.sdk.common.util.StringUtils;
import com.taobao.api.ApiException;
import com.taobao.api.response.TbkItemInfoGetResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.mall.BeanMapperUtil;
import org.jeecg.modules.appuser.entity.TbMnCollect;
import org.jeecg.modules.appuser.mapper.TbMnCollectMapper;
import org.jeecg.modules.appuser.service.ITbMnCollectService;
import org.jeecg.modules.mall.entity.TbMnMaterialOptional;
import org.jeecg.modules.mall.entity.TbMnProductDetail;
import org.jeecg.modules.mall.service.ITaobaoApiManager;
import org.jeecg.modules.mall.service.ITbMnMaterialOptionalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 用户商品收藏
 * @Author: jeecg-boot
 * @Date: 2020-05-19
 * @Version: V1.0
 */
@Service
public class TbMnCollectServiceImpl extends ServiceImpl<TbMnCollectMapper, TbMnCollect> implements ITbMnCollectService {

    @Autowired
    private ITaobaoApiManager iTaobaoApiManager;
    @Autowired
    private ITbMnMaterialOptionalService tbMnMaterialOptionalService;

    @Override
    public Result<?> collectProduct(TbMnCollect tbMnCollect) {

        if (null == tbMnCollect) {
            return Result.error("参数错误");
        }
        if (StringUtils.isEmpty(tbMnCollect.getUserId()) || StringUtils.isEmpty(tbMnCollect.getProductId())) {
            return Result.error("参数错误");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", tbMnCollect.getUserId());
        queryWrapper.eq("product_id", tbMnCollect.getProductId());
        TbMnCollect tbMnCollect1 = this.getOne(queryWrapper);
        if (null != tbMnCollect1) {
            if (this.removeById(tbMnCollect1)) {
                return Result.ok("取消收藏成功");
            }
        }
        if (this.save(tbMnCollect)) {
            return Result.ok("收藏成功");
        }
        return Result.error("收藏失败");
    }

    @Override
    public Result<?> getProductCollectAll(Integer pageNo, Integer pageSize) {
        Page<TbMnCollect> page = new Page<TbMnCollect>(pageNo, pageSize);
        IPage<TbMnCollect> tbMnCollectList = this.page(page);
        List<TbMnCollect> reocdetbMnCollectList = tbMnCollectList.getRecords();
        for (TbMnCollect tbMnCollect : reocdetbMnCollectList) {
            String projectid = tbMnCollect.getProductId();
            QueryWrapper materQueryWrapper = new QueryWrapper();
            materQueryWrapper.eq("num_iid", projectid);
            TbMnMaterialOptional tbMnMaterialOptional = tbMnMaterialOptionalService.getOne(materQueryWrapper);
            tbMnCollect.tbMnMaterialOptional = tbMnMaterialOptional;
        }
        tbMnCollectList.setRecords(reocdetbMnCollectList);
        return Result.ok(tbMnCollectList);
    }

    @Override
    public IPage<TbMnCollect> pageAndUserData(Page<TbMnCollect> page, TbMnCollect tbMnCollect) {
        return page.setRecords(this.baseMapper.pageAndUserData(page, tbMnCollect));
    }

    @Override
    public Result<?> getMyProductCollectAll(String user_id, Integer pageNo, Integer pageSize) throws ApiException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", user_id);
        Page<TbMnCollect> page = new Page<TbMnCollect>(pageNo, pageSize);

        IPage<TbMnCollect> tbMnCollectList = this.page(page, queryWrapper);
        List<TbMnCollect> reocdetbMnCollectList = tbMnCollectList.getRecords();
        for (TbMnCollect tbMnCollect : reocdetbMnCollectList) {
            String projectid = tbMnCollect.getProductId();
            QueryWrapper materQueryWrapper = new QueryWrapper();
            materQueryWrapper.eq("num_iid", projectid);
            TbMnMaterialOptional tbMnMaterialOptional = tbMnMaterialOptionalService.getOne(materQueryWrapper);
            tbMnCollect.tbMnMaterialOptional = tbMnMaterialOptional;
        }
        tbMnCollectList.setRecords(reocdetbMnCollectList);
        return Result.ok(tbMnCollectList);
    }
}
